//
//  IFMySchedulCell.m
//  Medicare
//
//  Created by admin on 2017/8/7.
//  Copyright © 2017年 medicare. All rights reserved.
//

#import "IFMySchedulCell.h"
#import "IFScheduStateView.h"
#import "IFSchedulDocModel.h"
#import "IFSchedulDocListModel.h"
#import "IFMySchedulingPublicDefines.h"
#import "ZSchedulingTypeModel.h"
#import "UIImage+ResetColor.h"
#import "UIImage+CircleImage.h"
#define offectDistanceX 5
@implementation IFMySchedulCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.stateViewArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < 8; i++) {
        IFScheduStateView * stateView;
        if (i == 0) {
            stateView  = [[IFScheduStateView alloc] initWithFrame:CGRectMake(0,(TABLEVIEWROWHEIGHT -  kScreenWidth/8.f)/2.f, kScreenWidth/8.f +offectDistanceX, kScreenWidth/8.f)];
            stateView.borderView.image = nil;
        } else {
            stateView  = [[IFScheduStateView alloc] initWithFrame:CGRectMake(kScreenWidth/8.f * i,(TABLEVIEWROWHEIGHT -  kScreenWidth/8.f)/2.f , kScreenWidth/8.f, kScreenWidth/8.f)];
            [stateView.clickBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        stateView.viewIndex = i;
        [self.stateViewArray addObject:stateView];
        [self addSubview:stateView];
    }
}

- (void)configForDocTorModel:(IFSchedulDocModel *)docModel AndIndexPath:(NSIndexPath *)indexPath {
    for (int i = 0; i < self.stateViewArray.count; i++) {
        if (i == 0) {;
            IFScheduStateView * stateView = self.stateViewArray[i];
            stateView.bigLabel.text = docModel.docName;
            stateView.bigLabel.numberOfLines = 2;
            
        } else {
            IFScheduStateView * stateView = self.stateViewArray[i];
            stateView.tag = 10 * indexPath.row + stateView.viewIndex;
            IFSchedulDocListModel * docListModel = docModel.classArrangementList[i - 1];
            stateView.isSelect = docListModel.isSelect;
            NSArray * scheduleArray,* colorArray;
            if (docListModel.classNames.length > 0) {
                scheduleArray = [docListModel.classNames componentsSeparatedByString:@","];
                if (docListModel.classColors.length > 0 ) {
                    colorArray = [docListModel.classColors componentsSeparatedByString:@","];
                } else {
                    
                }
            } else {
                
                
            }
            [self drewStateViewInterfaceWithScheduleArr:scheduleArray andWithColorArr:colorArray andWithStateView:stateView  andWithDocListModel:docListModel];
            if (!self.isEdit) {
                stateView.isSelect = NO;
                stateView.borderView.image =[Common getDocumentImageAndImageName:@"NoEditorPic"];
            } else {
                if (stateView.isSelect) {
                    stateView.borderView.image =[Common getDocumentImageAndImageName:@"EditorSelectPic"];
                } else {
                    stateView.borderView.image =[Common getDocumentImageAndImageName:@"EditorPic"];
                }
            }
        }
    }
    if (self.isEdit) {
        self.userInteractionEnabled = YES;
    } else {
        self.userInteractionEnabled = NO;
        
    }
}

- (void)drewStateViewInterfaceWithScheduleArr:(NSArray *)scheduleArray
                              andWithColorArr:(NSArray *)colorArray
                             andWithStateView:(IFScheduStateView *)view
                          andWithDocListModel:(IFSchedulDocListModel *)docListModel {
    // view.docListModel = docListModel;
    //view.isSelect = docListModel.isSelect;
    if (scheduleArray.count == 0) {
        view.bigLabel.hidden = NO;
        view.upLabel.hidden = YES;
        view.downLabel.hidden = YES;
        view.lineView.hidden = YES;
        view.bigLabel.text =  @"";
        view.upLabel.text = @"";
        view.downLabel.text = @"";
        view.scheduleType = SchedulingViewSpaceType;
        view.bigLabel.schedulingTypeModel = nil;
        view.upLabel.schedulingTypeModel = nil;
        view.downLabel.schedulingTypeModel = nil;
        
    } else if(scheduleArray.count == 1) {
        view.bigLabel.hidden = NO;
        view.bigLabel.textColor = COLORDIC[[colorArray firstObject]];
        view.upLabel.hidden = YES;
        view.downLabel.hidden = YES;
        view.bigLabel.text =  [[scheduleArray firstObject] length] > 3?[[scheduleArray firstObject] substringToIndex:3]:[scheduleArray firstObject];
        view.upLabel.text = @"";
        view.downLabel.text = @"";
        view.scheduleType = SchedulingViewOneLinesType;
        view.lineView.hidden = YES;
        view.bigLabel.schedulingTypeModel = [self buildZSchedulingAndWithClassInfoId:docListModel.classInfoIds AndWithDeptCode:docListModel.deptCode AndWithClassName:docListModel.classNames AndWithClassColor:docListModel.classColors];
        view.upLabel.schedulingTypeModel = nil;
        view.downLabel.schedulingTypeModel = nil;
        
    } else if(scheduleArray.count == 2) {
        view.bigLabel.hidden = YES;
        view.upLabel.hidden = NO;
        view.downLabel.hidden = NO;
        view.lineView.hidden = NO;
        view.upLabel.text =  [[scheduleArray firstObject] length] > 3?[[scheduleArray firstObject] substringToIndex:3]:[scheduleArray firstObject];
        view.downLabel.text =  [[scheduleArray lastObject] length] > 3?[[scheduleArray lastObject] substringToIndex:3]:[scheduleArray lastObject];
        view.upLabel.textColor =  COLORDIC[[colorArray firstObject]];
        view.downLabel.textColor =  COLORDIC[[colorArray lastObject]];
        view.bigLabel.text = @"";
        view.scheduleType = SchedulingViewTwoLinesType;
        view.bigLabel.schedulingTypeModel = nil;
        view.upLabel.schedulingTypeModel = [self buildZSchedulingAndWithClassInfoId:[[docListModel.classInfoIds componentsSeparatedByString:@","] firstObject]AndWithDeptCode:docListModel.deptCode AndWithClassName:[[docListModel.classNames componentsSeparatedByString:@","] firstObject] AndWithClassColor:[[docListModel.classColors componentsSeparatedByString:@","] firstObject]];
        view.downLabel.schedulingTypeModel = [self buildZSchedulingAndWithClassInfoId:[[docListModel.classInfoIds componentsSeparatedByString:@","] lastObject] AndWithDeptCode:docListModel.deptCode AndWithClassName:[[docListModel.classNames componentsSeparatedByString:@","] lastObject] AndWithClassColor:[[docListModel.classColors componentsSeparatedByString:@","] lastObject]];
    }
    docListModel.scheduleType = view.scheduleType;
    
}

- (ZSchedulingTypeModel *)buildZSchedulingAndWithClassInfoId:(NSString *)classInforId
                                             AndWithDeptCode:(NSString *)deptCode
                                            AndWithClassName:(NSString *)className
                                           AndWithClassColor:(NSString *)classColor {
    ZSchedulingTypeModel * typeModel = [[ZSchedulingTypeModel alloc] init];
    typeModel.className = className;
    typeModel.classInfoId = classInforId;
    typeModel.deptCode = deptCode;
    typeModel.color = classColor;
    return typeModel;
}

- (void)btnClick:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(btnSelectClick:)] && self.delegate) {
        [self.delegate btnSelectClick:sender];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
